package org.jackysoft.struts2;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.jackysoft.data.IDataProvider;
import org.jackysoft.data.ProviderNavigator;
import org.jackysoft.data.TransferProtocol;
import org.jackysoft.entity.Identifiable;
import org.jackysoft.util.Page;

import com.google.common.collect.Maps;
import com.opensymphony.xwork2.ActionContext;

/**
 * <p>
 * Action to test JQuery functionality.
 * </p>
 * 
 * @author Ernesto Reinaldo Barreiro (reirn70@gmail.com)
 * 
 */
public class SerializeAction<T extends Identifiable>
		extends AbstractAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7763763966584847700L;
	private final Log logger = LogFactory.getLog(SerializeAction.class);	
	protected Integer page=0;
	protected Integer rows=10;
	private String sord;
	private String sidx;
	private IDataProvider<?,T> dataProvider;	
	protected Map<String, Object> searchMap = Maps.newHashMap();
	protected Map<String, String> operMap = Maps.newHashMap();
	protected String[] usedProps = new String[0];
	private boolean listAll = false;

	public SerializeAction() {
		
		 
	}

	public String execute() throws Exception {
		setRequest_locale((String) ServletActionContext.getRequest()
				.getParameter("request_locale"));
		setMessage(getText(MESSAGE));
		return SUCCESS;
	}

	
	protected TransferProtocol getTransferProtocol() {
		return TransferProtocol.json;
	}

	public Page getData() throws Exception {
		logger.info("prepare grid data");
		Page pager = null;

		ProviderNavigator<T> navigator = new ProviderNavigator<T>(dataProvider,
				this.getTransferProtocol(), searchMap);
		navigator.setPage(page);
		navigator.setRows(rows);
		navigator.setSidx(sidx);
		navigator.setSord(sord);
		navigator.setListAll(isListAll());
		pager = navigator.renderData();
		logger.info("grid data got ");
		return pager;
	}

	public String getJson() throws Exception {
		this.pager = getData();
		return SUCCESS;
	}

	/**
	 * Provide default valuie for Message property.
	 */
	public static final String MESSAGE = "HelloWorld.message";

	/**
	 * Field for Message property.
	 */
	private String message;

	/**
     * 
     */
	private String request_locale;

	/**
	 * Return Message property.
	 * 
	 * @return Message property
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * Set Message property.
	 * 
	 * @param message
	 *            Text to display on HelloWorld page.
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the request_locale
	 */
	public String getRequest_locale() {
		return request_locale;
	}

	/**
	 * @param request_locale
	 *            the request_locale to set
	 */
	public void setRequest_locale(String request_locale) {
		this.request_locale = request_locale;
		if (this.request_locale != null && this.request_locale.equals("en")) {
			ActionContext.getContext().setLocale(Locale.ENGLISH);
		}
	}

	public IDataProvider<?,T> getDataProvider() {
		return dataProvider;
	}

	public void setDataProvider(IDataProvider<?,T> dataProvider) {
		this.dataProvider = dataProvider;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getPage() {
		return page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getRows() {
		return rows;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSord() {
		return sord;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSidx() {
		return sidx;
	}


	public void setListAll(boolean listAll) {
		this.listAll = listAll;
	}

	public boolean isListAll() {
		return listAll;
	}

	public Map<String, Object> getSearchMap() {
		searchMap.put("operation", operMap);
		return searchMap;
	}

	public void setSearchMap(Map<String, Object> searchMap) {
		this.searchMap = searchMap;
	}

	public Map<String, String> getOperMap() {
		return operMap;
	}

	public void setOperMap(Map<String, String> operMap) {
		this.operMap = operMap;
	}

	public void setUsedProps(String[] usedProps) {
		this.usedProps = usedProps;
	}

	public String[] getUsedProps() {
		return usedProps;
	}
	
}
